//
//  TicketRecordVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/12.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "TicketRecordVc.h"
#import "TicketRecordCell.h"
#import "TicketRecordDetailVc.h"
#import "NewPersonalNetWork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "TicketRecordModel.h"

@interface TicketRecordVc ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *tickets;
@property (weak, nonatomic) IBOutlet UILabel *leftCountLab;

@end

@implementation TicketRecordVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"使用记录";
    self.view.backgroundColor = RGBColor(238, 238, 244);
    self.tableView.backgroundColor = RGBColor(238, 238, 244);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TicketRecordCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TicketRecordCell class])];
    self.leftCountLab.text = [NSString stringWithFormat:@"- 剩余%zd次 -",self.model.num];
    [self getTicketRecord];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tickets.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketRecordModel *model = self.tickets[indexPath.row];
    float textH = [model.clubCommodityName boundingRectWithSize:CGSizeMake(ScreenW-60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:Font(16)} context:nil].size.height;
    return 90+textH;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TicketRecordCell class])];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.tickets[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketRecordDetailVc *vc = [[TicketRecordDetailVc alloc]init];
    vc.model = self.tickets[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)getTicketRecord
{
    NSMutableDictionary *paremes = [NSMutableDictionary dictionary];
    paremes[@"token"] = [SZLAccountHelper shareInstance].token;
    paremes[@"ticketNo"] = self.model.ticketNo;
    [NewPersonalNetWork getTicketRecordParame:paremes response:^(BOOL success, NSString *msg, NSArray *lists) {
        if (success) {
            if (lists.count == 0) {
                [self showNoDataLab:self.tableView range:CGRectMake(0, self.tableView.height*0.5, ScreenW, 20) title:@"暂无数据"];
                return ;
            }
            [self.tickets addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

- (NSMutableArray *)tickets
{
    if (!_tickets) {
        _tickets = [NSMutableArray array];
    }
    return _tickets;
}

@end
